home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / tblt / tblt⁄cntl.c < prev    next >
Encoding:
Text File  |  1986-09-06  |  3.0 KB  |  119 lines  |  [TEXT/MACA]

  1. /*
  2.  * cntl.c - control handling for the game of Tablut
  3.  */
  4.  
  5. #include <quickdraw.h>
  6. #include <window.h>
  7. #include <control.h>
  8. #define ALLDIM 255        /* should be in control.h, but isn't */
  9. #include <toolutil.h>
  10. #include <memory.h>
  11. #include <resource.h>
  12.  
  13. #include "tablut.h"
  14.  
  15. #define PLAYCNTL    129    /* Resource ID of the "Play" radio button */
  16. #define STOPCNTL    130    /* Resource ID of the "Stop" radio button */
  17. #define LOOKCNTL    131    /* Resource ID of the game scrollbar      */
  18.  
  19. ControlHandle playbutton;    /* the Play radio button        */
  20. ControlHandle stopbutton;    /* the Stop radio button        */
  21. ControlHandle looker;        /* the Look (move browser) scrollbar    */
  22.  
  23. /*
  24.  * getcontrols() - initialize the game controls
  25.  */
  26. getcontrols()
  27. {
  28.     playbutton = GetNewControl(PLAYCNTL, mywindow);
  29.     stopbutton = GetNewControl(STOPCNTL, mywindow);
  30.     looker     = GetNewControl(LOOKCNTL, mywindow);
  31. }
  32.  
  33. /*
  34.  * docontrol() - handle a mouse-click in one of our controls
  35.  */
  36. docontrol(cntl, part, placep)
  37. ControlHandle cntl;    /* the control that was pressed        */
  38. short part;        /* the pressed part of that control    */
  39. Point *placep;        /* where the mouse was when pressed    */
  40. {
  41.     short n;
  42.     pascal void trackbrowse();
  43.  
  44.     if (cntl == playbutton || cntl == stopbutton) {
  45.     if (TrackControl(cntl, pass(*placep), (ProcPtr) 0)) {
  46.         setsetup(cntl == stopbutton);
  47.     }
  48.     } else if (cntl == looker) {
  49.     if (part == inThumb) {
  50.         setsetup(1); /* scrolling stops the game */
  51.         if (TrackControl(cntl, pass(*placep), (ProcPtr) 0)) {
  52.         n = GetCtlValue(looker);
  53.         seekboard(n - 1);
  54.         readboard();
  55.         }
  56.     } else {
  57.         setsetup(1);    /* scrolling stops the game */
  58.         (void) TrackControl(cntl, pass(*placep), trackbrowse);
  59.     }
  60.     }
  61. }
  62.  
  63. /*
  64.  * trackbrowse() - "scroll" through the game as long as the mouse is down
  65.  */
  66. pascal void
  67. trackbrowse(cntl, part)
  68. ControlHandle cntl;    /* the touched control    */
  69. short part;        /* the touched part    */
  70. {
  71.     short delta;        /* number of moves to skip forward or back  */
  72.  
  73.     switch (part) {
  74.     case inUpButton: delta = -1; break;
  75.     case inDownButton: delta = 1; break;
  76.     case inPageUp: delta = -2; break;
  77.     case inPageDown: delta = 2; break;
  78.     default: delta = 0; break;
  79.     }
  80.     if (delta) {
  81.     seekboard(curmove - 1 + delta); readboard();
  82.     }
  83. }
  84.  
  85. /*
  86.  * setsetup() - put a value into the Play/Stop state & set the button
  87.  *  accordingly: If the game is being played, set "Stop"; otherwise, "Play".
  88.  */
  89. setsetup(newval)
  90. int newval;
  91. {
  92.     static int first = 1;    /* true if the first time through    */
  93.  
  94.     if (winner != NOWIN) newval = 1;    /* can't play if somebody has won */
  95.     if (first || insetup != newval) {
  96.     first = 0;
  97.     insetup = newval;
  98.     SetCtlValue(playbutton, !insetup);
  99.     SetCtlValue(stopbutton,  insetup);
  100.     }
  101. }
  102.  
  103. /*
  104.  * fixlooker() - make the scrollbar reflect the current state of the game
  105.  */
  106. fixlooker()
  107. {
  108.     SetCtlMin(looker, 1);
  109.     if (nummoves < 2) {
  110.     HiliteControl(looker, ALLDIM);
  111.     } else {
  112.     HideControl(looker);
  113.     SetCtlMax(looker, nummoves);
  114.     SetCtlValue(looker, curmove);
  115.     HiliteControl(looker, 0);
  116.     ShowControl(looker);
  117.     }
  118. }
  119.